package com.cimr.comm.velocity;

import com.cimr.comm.Constants;
import com.cimr.comm.service.PermissionService;
import com.cimr.comm.token.TokenUtil;
import com.cimr.comm.util.SpringContextUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.tools.view.ViewToolContext;

import java.io.IOException;
import java.io.Writer;

/**
 * Created by liqi on 2017/12/15.
 * liqiwork@qq.com
 */
public class HasPermissionDirective extends Directive {
    @Override
    public String getName() {
        return "hasPermission";
    }

    @Override
    public int getType() {
        return 2;
    }

    @Override
    public boolean render(InternalContextAdapter internalContextAdapter, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {

        ViewToolContext context = (ViewToolContext) internalContextAdapter.getInternalUserContext();
        return doRender(internalContextAdapter, context, writer, node);
    }

    protected boolean doRender(InternalContextAdapter internalContext,
                               ViewToolContext context, Writer writer, Node node)
            throws IOException, ResourceNotFoundException, ParseErrorException,
            MethodInvocationException {

        SimpleNode snKey = (SimpleNode) node.jjtGetChild(0);
        if (TokenUtil.isAdministrator() || SecurityUtils.getSubject().isPermitted(Constants.OPERATION_PASSKEY)) {
//        if (TokenUtil.isAdministrator()) {
            SimpleNode snContent = (SimpleNode) node.jjtGetChild(1);
            String content = (String) snContent.value(internalContext);
            writer.write(content);
            return true;
        }

        String key = (String) snKey.value(internalContext);
        String userId = TokenUtil.getUserId();
        if (SecurityUtils.getSubject().isPermitted(key)) {
            SimpleNode snContent = (SimpleNode) node.jjtGetChild(1);
            String content = (String) snContent.value(internalContext);
            writer.write(content);
//        }
//        Set permissionSet = permissionService.findPermissionByUserId(userId);
//        if (userId!=null && permissionSet.contains(key)) {
//            SimpleNode snContent = (SimpleNode) node.jjtGetChild(1);
//            String content = (String) snContent.value(internalContext);
//            writer.write(content);
        } else {
            writer.write("");
        }
        return true;
    }

    public static final PermissionService permissionService = SpringContextUtil.getBean("permissionService",PermissionService.class);
//    @Autowired
//    private PermissionService permissionService;
}
